package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.PageResult;
import com.woniu.pojo.CarouselChart;
import com.woniu.pojo.CarouselChart;
import com.woniu.mapper.CarouselChartMapper;
import com.woniu.pojo.Medicine;
import com.woniu.service.ICarouselChartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.vo.CarouselChartQueryVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * <p>
 * 轮播图管理表 服务实现类
 * </p>
 *
 * @author 刘岩
 * @since 2022-10-14
 */
@Service
public class CarouselChartServiceImpl extends ServiceImpl<CarouselChartMapper, CarouselChart> implements ICarouselChartService {

    @Resource
    private CarouselChartMapper chartMapper;
    
    /**
     * 条件搜索分页查询
     * @param currentPageStr
     * @param pageSizeStr
     * @param chartQueryVo
     * @return
     */
    @Override
    public PageResult queryByPage(String currentPageStr, String pageSizeStr, CarouselChartQueryVo chartQueryVo) {
        //转int
        int currentPage = Integer.parseInt(currentPageStr);
        int pageSize = Integer.parseInt(pageSizeStr);
        PageResult pageResult = new PageResult();
        //构造分页
        IPage<CarouselChart> page=new Page(currentPage,pageSize);
        //如果搜索条件不为空
        if (chartQueryVo!=null){
            QueryWrapper<CarouselChart> wrapper=new QueryWrapper<>();
            if(chartQueryVo.getCreateTime() != null){
                wrapper.gt("create_time", chartQueryVo.getCreateTime());
            }
            if(chartQueryVo.getUpdateTime() != null){
                wrapper.lt("create_time", chartQueryVo.getUpdateTime());
            }
            if (chartQueryVo.getTitle()!=null){
                wrapper.like("title",chartQueryVo.getTitle());
            }
            //查询
            IPage<CarouselChart> carouselChartIPage = chartMapper.selectPage(page, wrapper);
            pageResult.setTotal(carouselChartIPage.getTotal());
            pageResult.setData(carouselChartIPage.getRecords());
            return pageResult;
        }
        //如果没有查询条件
        IPage<CarouselChart> carouselChartIPage = chartMapper.selectPage(page, null);
        pageResult.setTotal(carouselChartIPage.getTotal());
        pageResult.setData(carouselChartIPage.getRecords());
        return pageResult;
    }

    /**
     * 查询轮播图排序的最大值
     * @return
     */
    @Override
    public Integer queryMaxSortByCourseId() {
        return chartMapper.selectMaxSortByCourseId();
    }

    /**
     * 修改轮播图发布状态
     * @param carouselId
     * @return
     */
    @Override
    public boolean saveStatus(Long carouselId) {
        UpdateWrapper<CarouselChart> updateWrapper = new UpdateWrapper<>();
        CarouselChart carouselChart = chartMapper.selectById(carouselId);
        if(!StringUtils.isEmpty(carouselChart.getStatus()) && carouselChart.getStatus().equals("0")){
            updateWrapper.set("status", "1");
            updateWrapper.eq("id", carouselId);
            return update(updateWrapper);
        }
        if(!StringUtils.isEmpty(carouselChart.getStatus()) && carouselChart.getStatus().equals("1")){
            updateWrapper.set("status", "0");
            updateWrapper.eq("id", carouselId);
            return update(updateWrapper);
        }
        return false;
    }
}
